package com.mdnote.practice.dp.stock;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/9/22
 * @description 买卖股票的最佳时机含手续费
 */
public class LeetCode714 {

    public static void main(String[] args) {
        LeetCode714 leetCode714 = new LeetCode714();
        int res = leetCode714.maxProfit(new int[]{1, 3, 2, 8, 4, 9}, 2);
        System.out.println(res);
    }


    public int maxProfit(int[] prices, int fee) {
        if (prices.length <= 1) {
            return 0;
        }

        int[][] dp = new int[prices.length][2];

        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for (int i = 1; i < dp.length; i++) {

            // 当天手上没有股票，要么昨天就没有，要么昨天卖了
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
            // 当天手上有股票，应该是昨天买的或者昨天就有了
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }
        return dp[dp.length - 1][0];
    }


}
